package backpage.control;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.User;
import com.opensymphony.xwork2.util.ResolverUtil.IsA;

import backpage.model.DBAccessor;
import backpage.view.PageDivision;
import backpage.view.ResItem;

public class onPageChange extends HttpServlet {
	private static final long serialVersionUID = -766811024246843654L;
	/**
	 * Constructor of the object.
	 */
	public onPageChange() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("userInfo");
		if (user == null || !user.getState().equals("teacher")){
			response.sendRedirect("backPage/jsp/login.jsp");
			return;
		}
		
		
		String resType = request.getParameter("resType");
		String sortKey = request.getParameter("sortKey");
		String upOrDown ;
		String pageCount = request.getParameter("numInOnePage");
		String curPage = request.getParameter("curPage");
		
		if( resType == null || resType.equals("null") )
			resType = "doc";
		if( sortKey == null || sortKey == ""){
			sortKey = "file_name";
			upOrDown = "asc";
		}else{
			if( sortKey.charAt(0) == '1' )
				upOrDown = "desc";
			else upOrDown = "asc";
			if( sortKey.substring(1).equals("fileName") )
				sortKey = "file_name";
			else if( sortKey.substring(1).equals("createTime") )
				sortKey = "create_time";
			else if( sortKey.substring(1).equals("downloadNum") )
				sortKey = "download_num";
			else if( sortKey.substring(1).equals("touchNum") )
				sortKey = "touch_num";
		}
		if( pageCount == null )
			pageCount = "20";
		if( curPage == null )
			curPage = "1";
		//System.out.println("resType:"+new String(resType.getBytes())+"  pageCount:"+pageCount+"  curpage:"+curPage);

		PageDivision pageDivision = new PageDivision(0, 0, new ArrayList<ResItem>());;
		if( request.getParameter("search") == null )
			pageDivision = DBAccessor.Instance.getPageDivision(resType, sortKey, upOrDown , pageCount, curPage);
		else if( request.getParameter("search").equals("true") ){
			String wordScope = request.getParameter("searchScope");
			if( wordScope == null ) wordScope = "all";	//for all types
			String keyWord = request.getParameter("keyWord");
			if( keyWord == null ) keyWord = "";
			String keyType = request.getParameter("wordType");
			if( keyType == null ) keyType = "all";
			pageDivision = 
				DBAccessor.Instance.doSearch(wordScope, sortKey, upOrDown, keyWord, keyType, pageCount, curPage);
		}
		
		request.setAttribute("pageDivision", pageDivision);
		
		//increase the touch_num, if needed
		ArrayList<String> touchNumList = null;
		if( request.getSession().getAttribute("touchNumList") != null)
			touchNumList = (ArrayList<String>)request.getSession().getAttribute("touchNumList");
		else touchNumList = new ArrayList<String>();
		for( int i=0; i<pageDivision.getitemList().size(); i++ ){	//for each requested ResItem
			int id = ( (ResItem)pageDivision.getitemList().get(i) ).getID();
			if( !touchNumList.contains( Integer.toString(id) ) ){	//not requested before
				touchNumList.add(Integer.toString(id));
				DBAccessor.Instance.incTouchNum(Integer.toString(id));
			}
		}
		request.getSession().setAttribute("touchNumList", touchNumList);		//write the touched IDs back to the session
		
		RequestDispatcher rDispatcher;
		if( request.getParameter("search") == null ){
			if( request.getParameter("resType") == null )
				rDispatcher = request.getRequestDispatcher("/backPage/index.jsp?resType=doc");
			else rDispatcher = request.getRequestDispatcher("/backPage/index.jsp");
			if( rDispatcher != null ) rDispatcher.forward(request, response);
		}
		else if( request.getParameter("search").equals("true") ){
			rDispatcher = request.getRequestDispatcher("/backPage/jsp/search.jsp");
			if( rDispatcher != null ) rDispatcher.forward(request, response);
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
